package com.hzit.bj1804.dianweb.controller;


import com.github.pagehelper.PageInfo;
import com.hzit.bj1804.dian.entity.Productcategory;
import com.hzit.bj1804.dian.vo.LayuiTableData;
import com.hzit.bj1804.dian.vo.ResponseVo;
import com.hzit.bj1804.dianweb.service.ProductategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @program: dian
 * @description:
 * @author: zhz
 * @create: 2018-12-01 15:26
 **/
@Controller
public class ProductategoryController {
    @Autowired
    private ProductategoryService productategoryService;
//    @Autowired
//    private OSSClient ossClient;
    /**
     * 进入主页
     * @return
     */
//    @RequestMapping("/")
//    public String testPro(){
//        System.out.println("进入--->主页");
//        return "moban";
//    }
    @RequestMapping("getProgory")
    public String getPro(){
        System.out.println("进入--->主页");
        return "netshop/category_list";
    }
    /**
     * 分页查询
     * @param limit
     * @param page
     * @returnr
     */
    @RequestMapping("category_list_data")
    @ResponseBody
    public Object getAllProductategoryByPage(@RequestParam(value = "limit",defaultValue = "8") int limit,@RequestParam(value = "page",defaultValue = "1") int page){
        //System.out.println("xxxx");
        LayuiTableData data = new LayuiTableData();
        PageInfo<Productcategory> allType = productategoryService.getAllTypeByPage(limit, page);
        List<Productcategory> allProductcategoryType = productategoryService.getAllProductcategoryType();
        System.out.println("A:"+allType);
        List<Productcategory> list = allType.getList();
        data.setData(list);
        data.setCode(0);
//        System.out.println(allType.getTotal());
        data.setCount((int)allType.getTotal());
        data.setMsg("");
        return data;
    }
    /**
     * 新增跳转至新增页面
     */
    @RequestMapping("/addcategory")
    public String addProductategoryType(){
        return "netshop/addcategory";
    }
    /**
     * 新增类别信息
     * @param productcategory
     * @return
     */
    @RequestMapping("/addCategroy")
    @ResponseBody
    public Object addCategoryType(@RequestBody Productcategory productcategory ){
        int i = productategoryService.addType(productcategory);
        System.out.println("添加------>"+i);
        return i;
    }
//    @RequestMapping("addTupian")
//    @ResponseBody
//    public Object addTuPianFile(@RequestParam("file")CommonsMultipartFile file){
//        ResponseVo vo = new ResponseVo();
//        String path="D:\\IdeaProjects\\Bj1804D\\dian\\dian-web\\src\\main\\resources\\ProductategoryImg";
//        String filename = file.getOriginalFilename();
//        String substring = filename.substring(filename.indexOf("."));
//        String uuid = UUID.randomUUID().toString() + substring;
//        File newFile = new File(path + "/" + uuid);
//        try {
//            file.transferTo(newFile);
//            System.out.println("文件已经上传：--->"+newFile.getAbsolutePath());
//            ossClient.putObject("page-bj1804",uuid,newFile);
//            String newstr = ossClient.getEndpoint().toString().substring(ossClient.getEndpoint().toString().indexOf("oss-"));
//            String url="http://page-bj1804."+newstr+"/"+uuid;
//            vo.setData(url);
//            vo.setMag("OK");
//        } catch (IOException e) {
//            e.printStackTrace();
//            vo.setCode(1);
//            vo.setData(e.getMessage());
//        }
//        return vo;
//    }
    /**
     * 根据ID查询数据,跳转到修改页面
     * @param categoryid
     * @param map
     * @return
     */
    @RequestMapping("/getCategoryById")
    public Object getCategoryById(@RequestParam("categoryid")int categoryid,ModelMap map){
        List<Productcategory> categoryById = productategoryService.getCategoryById(categoryid);
        if (categoryById!=null){
            System.out.println(categoryById);
        }
        map.put("categoryById",categoryById);
        System.out.println(map);
        return "netshop/updatecategroy";
    }

    /**
     * 修改功能的控制器
     * @param productcategory
     * @return
     */
    @RequestMapping("/goUpdate")
    @ResponseBody
    public Object goUpdate(@RequestBody Productcategory productcategory){
        int i = productategoryService.upType(productcategory);
        System.out.println("修改----->"+i);
        return i;
    }
    /**
     * 修改状态
     * @param categoryid
     * @return
     */
    @RequestMapping("/updateCategory")
    @ResponseBody
    public Object updateCategoryById(@RequestParam("categoryid")int categoryid){
        int i = productategoryService.delUpStatus(categoryid);
        return i;
    }

    /**
     * 根据Id查询类别
     * @param categoryid
     * @return
     */
    @RequestMapping("/getBy_Id")
    @ResponseBody
    public Object getProdById(@RequestParam("categoryid") int categoryid){
        System.out.println("AAA:"+categoryid);
        List<Productcategory> byId = productategoryService.getCategoryById(categoryid);
        System.out.println(byId);
        LayuiTableData data = new LayuiTableData();
        data.setMsg("");
        data.setCode(0);
        data.setCount(1);
        data.setData(byId);
        return data;
    }
    @RequestMapping("/ByIdAndName")
    @ResponseBody
    public Object getProdByIdAndName(@RequestParam("categoryname") String categoryname){
        System.out.println("类别名-------》"+categoryname);
        List<Productcategory> byIdAndName = productategoryService.getByIdAndName(categoryname);
        System.out.println(byIdAndName);
        LayuiTableData data = new LayuiTableData();
        data.setData(byIdAndName);
        data.setCount(byIdAndName.size());
        data.setMsg("");
        data.setCode(0);
        return data;
    }

}
